package org.example.controller;

import org.example.bean.R;
import org.example.dto.VideoParseRecordDTO;
import org.example.dto.VideoParseStatsDTO;
import org.example.service.VideoParseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/video-parse")
public class VideoParseRecordController {
    
    @Autowired
    private VideoParseRecordService videoParseRecordService;
    
    /**
     * 查询所有记录
     */
    @GetMapping("/records")
    public R getAllRecords() {
        try {
            List<VideoParseRecordDTO> records = videoParseRecordService.getAllRecords();
            return new R().setMsg("查询成功").add("data", records).add("total", records.size());
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据昵称查询记录
     */
    @GetMapping("/records/nickname")
    public R getRecordsByNickname(@RequestParam String nickname) {
        try {
            List<VideoParseRecordDTO> records = videoParseRecordService.getRecordsByNickname(nickname);
            return new R().setMsg("查询成功").add("data", records).add("total", records.size());
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据解析状态查询记录
     */
    @GetMapping("/records/status")
    public R getRecordsByStatus(@RequestParam Integer parseStatus) {
        try {
            List<VideoParseRecordDTO> records = videoParseRecordService.getRecordsByParseStatus(parseStatus);
            return new R().setMsg("查询成功").add("data", records).add("total", records.size());
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据平台查询记录
     */
    @GetMapping("/records/platform")
    public R getRecordsByPlatform(@RequestParam String platform) {
        try {
            List<VideoParseRecordDTO> records = videoParseRecordService.getRecordsByPlatform(platform);
            return new R().setMsg("查询成功").add("data", records).add("total", records.size());
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据昵称和解析状态查询记录
     */
    @GetMapping("/records/nickname-status")
    public R getRecordsByNicknameAndStatus(
            @RequestParam String nickname,
            @RequestParam Integer parseStatus) {
        try {
            List<VideoParseRecordDTO> records = videoParseRecordService.getRecordsByNicknameAndStatus(nickname, parseStatus);
            return new R().setMsg("查询成功").add("data", records).add("total", records.size());
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据昵称和平台查询记录
     */
    @GetMapping("/records/nickname-platform")
    public R getRecordsByNicknameAndPlatform(
            @RequestParam String nickname,
            @RequestParam String platform) {
        try {
            List<VideoParseRecordDTO> records = videoParseRecordService.getRecordsByNicknameAndPlatform(nickname, platform);
            return new R().setMsg("查询成功").add("data", records).add("total", records.size());
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询成功解析的记录
     */
    @GetMapping("/records/success")
    public R getSuccessRecords() {
        try {
            List<VideoParseRecordDTO> records = videoParseRecordService.getSuccessRecords();
            return new R().setMsg("查询成功").add("data", records).add("total", records.size());
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询失败解析的记录
     */
    @GetMapping("/records/failed")
    public R getFailedRecords() {
        try {
            List<VideoParseRecordDTO> records = videoParseRecordService.getFailedRecords();
            return new R().setMsg("查询成功").add("data", records).add("total", records.size());
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 分页查询记录
     */
    @GetMapping("/records/page")
    public R getRecordsWithPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            List<VideoParseRecordDTO> records = videoParseRecordService.getRecordsWithPage(page, size);
            return new R().setMsg("查询成功").add("data", records).add("page", page).add("size", size);
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取统计信息
     */
    @GetMapping("/stats")
    public R getStats() {
        try {
            VideoParseStatsDTO stats = videoParseRecordService.getStats();
            return new R().setMsg("查询成功").add("data", stats);
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据昵称获取统计信息
     */
    @GetMapping("/stats/nickname")
    public R getStatsByNickname(@RequestParam String nickname) {
        try {
            VideoParseStatsDTO stats = videoParseRecordService.getStatsByNickname(nickname);
            return new R().setMsg("查询成功").add("data", stats);
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据平台获取统计信息
     */
    @GetMapping("/stats/platform")
    public R getStatsByPlatform(@RequestParam String platform) {
        try {
            VideoParseStatsDTO stats = videoParseRecordService.getStatsByPlatform(platform);
            return new R().setMsg("查询成功").add("data", stats);
        } catch (Exception e) {
            return new R().setCode(500).setMsg("查询失败: " + e.getMessage());
        }
    }
} 